﻿using System.Collections.Generic;
using ModelingProjectLib.Interfaces;
using ModelingProjectLib;
using System.Diagnostics;
using System.Collections.ObjectModel;

namespace INSAwarsWPF
{
    public class CaseViewModel : ViewModelBase
    {
        public Case CaseModel { get; set; }

        public GameViewModel Conteneur { get; private set; }
        public static string[] tabCouleur = { "Blue", "Red", "Yellow", "Green" };
        public static string[] tabCouleurCode = { "#275fd8", "#c71b10", "#e7e413", "#209400" };

        public CaseViewModel(GameViewModel _gvm, int _row, int _col, Case _case)
        {
            Row = _row;
            Column = _col;
            Unites = new ObservableCollection<UniteViewModel>();
            VilleCase = null;
            Conteneur = _gvm;

            CaseModel = _case;

            if (_case is DecorateurFer)
            {
                Ressource = "Fer";
                IsDecorated = true;
                TextureDeco = "/INSAwarsWPF;component/Images/Terrains/fer.png";
                Texture = getTextureString(((DecorateurFer)_case).CaseDecoree);
                Terrain = getTerrainString(((DecorateurFer)_case).CaseDecoree);
            }
            else if (_case is DecorateurFruit)
            {
                Ressource = "Fruit";
                IsDecorated = true;
                TextureDeco = "/INSAwarsWPF;component/Images/Terrains/fruit.png";
                Texture = getTextureString(((DecorateurFruit)_case).CaseDecoree);
                Terrain = getTerrainString(((DecorateurFruit)_case).CaseDecoree);
            }
            else
            {
                Ressource = "Pas de ressource";
                IsDecorated = false;
                TextureDeco = "";
                Texture = getTextureString(_case);
                Terrain = getTerrainString(_case);
            }
        }

        private string getTextureString(Case _case)
        {
            if (_case is CaseDesert) return "/INSAwarsWPF;component/Images/Terrains/desert.png";
            else if (_case is CaseMontagne) return "/INSAwarsWPF;component/Images/Terrains/montagne.png";
            else if (_case is CasePlaine) return "/INSAwarsWPF;component/Images/Terrains/plaine.png";
            else return "";
        }

        private string getTerrainString(Case _case)
        {
            if (_case is CaseDesert) return "Desert";
            else if (_case is CaseMontagne) return "Montagne";
            else if (_case is CasePlaine) return "Plaine";
            else return "";
        }

        public string Terrain { get; private set; }
        public string Ressource { get; private set; }
        public int Row { get; private set; }
        public int Column { get; private set; }
        public string Texture { get; private set; }
        public string TextureDeco { get; private set; }
        public bool IsDecorated { get; set; }
        public bool IsSousInfluenceDeVille { get; set; } // a faire plus tard...

        public ObservableCollection<UniteViewModel> Unites { get; set; }

        private VilleViewModel villeCase;
        public VilleViewModel VilleCase { get { return villeCase; }
            set { villeCase = value;
                RaisePropertyChanged("VilleCase");
                RaisePropertyChanged("HasVille");
                RaisePropertyChanged("IsVilleCreable");
                RaisePropertyChanged("HasUnite");
            }
        }

        private bool isSelected;
        public bool IsSelected {
            get { return isSelected; }
            set { isSelected = value; RaisePropertyChanged("IsSelected"); }
        }

        private UniteViewModel selectedUnite;
        public UniteViewModel SelectedUnite
        {
            get { return selectedUnite; }

            set
            {
                if (selectedUnite != value)
                {
                    if (selectedUnite != null) selectedUnite.IsSelected = false;
                    selectedUnite = value;
                    if (selectedUnite != null) selectedUnite.IsSelected = true;
                    Conteneur.SelectedCase = this;
                    RaisePropertyChanged("SelectedUnite");
                    RaisePropertyChanged("HasUnite");
                    RaisePropertyChanged("IsVilleCreable");
                }
            }
        }

        public bool HasUnite { get { return SelectedUnite != null; } }
        public bool HasVille { get { return VilleCase != null; } }

        public bool IsVilleCreable { get { return HasUnite && SelectedUnite.UniteModel.Type == "Enseignant" && !HasVille; } }

        public void majChangementJoueur()
        {
            RaisePropertyChanged("Unites");
        }
    }
}
